#import "ZGLShufflingViewCell.h"
#import <UIImageView+AFNetworking.h>
#import "ZGLHTTPTOOL.h"
#import "ZGLShufflingViewModel.h"

@interface ZGLShufflingViewCell () <UIScrollViewDelegate>
@property (nonatomic, strong) UIScrollView *ShufflingScr;
@property (nonatomic, strong) NSArray *dicArr;
@property (nonatomic, retain) NSTimer *timer;

@end

@implementation ZGLShufflingViewCell

static long imgNum = 4;//轮播图数量默认为4, handledata后修改

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.contentView.backgroundColor = [UIColor lightGrayColor];
        _dicArr = [[NSArray alloc]initWithObjects:@{}, @{}, @{}, @{}, nil];
        [self handleData];
    }
    return self;
}

- (void)handleData{
    [ZGLHTTPTOOL GETWithURL:@"http://iphone.myzaker.com/zaker/follow_promote.php?_appid=AndroidPhone&_bsize=1080_1920&_dev=515&_lat=38.889582&_lng=121.550716&_mac=68%3Aa0%3Af6%3Ae5%3A55%3A9f&_mcode=84669885&_net=wifi&_nudid=20ebfb1da30a114d&_os=4.4.2_H60-L03&_os_name=H60-L03&_udid=865473021901486&_v=6.4.1&_version=6.41&m=1446016735" withBody:nil withHttpHead:nil responseStyle:JSON withSuccess:^(id result) {
        
        NSDictionary *dic = result[@"data"];
        _dicArr = [[NSArray alloc]initWithArray:dic[@"list"]];
        imgNum = _dicArr.count;
        [self reloadShufflingScr];
        [self addTimer];
    } withFail:^(id result) {
    }];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    [self reloadShufflingScr];
}

- (void)reloadShufflingScr {
    if (_ShufflingScr) {
        [_ShufflingScr removeFromSuperview];
    }
    _ShufflingScr = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, Width, ScaleY * 200)];
    _ShufflingScr.delegate = self;
    _ShufflingScr.scrollEnabled = YES;
    _ShufflingScr.pagingEnabled = YES;
    _ShufflingScr.bounces = NO;
    _ShufflingScr.showsHorizontalScrollIndicator = NO;
    _ShufflingScr.showsVerticalScrollIndicator = NO;
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(click:)];
    [_ShufflingScr addGestureRecognizer:tapGesture];
    [self.contentView addSubview:_ShufflingScr];
    
    self.ShufflingScr.contentSize = CGSizeMake(Width * (imgNum+2),0);
    for (NSInteger i = 0; i < (imgNum+2); i++) {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(Width*i, 0, Width, self.frame.size.height)];
        imageView.backgroundColor = [UIColor redColor];
        [self.ShufflingScr addSubview:imageView];
        
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(ScaleX * 20 + Width*i, self.frame.size.height - ScaleY * 40, Width - ScaleX * 40, ScaleY * 40)];
        label.textColor = [UIColor whiteColor];
        label.shadowColor = [UIColor blackColor];
        label.shadowOffset = CGSizeMake(0, -0.5);
        [self.ShufflingScr addSubview:label];
    }
    [self setImages];
}

- (void)setImages {
    ZGLShufflingViewModel *model = [ZGLShufflingViewModel modWithDic:_dicArr[imgNum - 1]];
    UIImageView *imageView_first = [self.ShufflingScr.subviews objectAtIndex:0];
    [imageView_first setImageWithURL:[NSURL URLWithString:model.promotion_img] placeholderImage:[UIImage imageNamed:@"nopic"]];
    UILabel *label_first= [self.ShufflingScr.subviews objectAtIndex:1];
    label_first.text = model.title;
    model = [ZGLShufflingViewModel modWithDic:_dicArr[0]];
    UIImageView *imageView_last = [self.ShufflingScr.subviews objectAtIndex:imgNum*2+2];
    [imageView_last setImageWithURL:[NSURL URLWithString:model.promotion_img] placeholderImage:[UIImage imageNamed:@"nopic"]];
    UILabel *label_last= [self.ShufflingScr.subviews objectAtIndex:imgNum*2+3];
    label_last.text = model.title;
    
    for (NSInteger i = 0; i < imgNum; i++) {
        model = [ZGLShufflingViewModel modWithDic:_dicArr[i]];
        UIImageView *imageView = [self.ShufflingScr.subviews objectAtIndex:i*2+2];
        [imageView setImageWithURL:[NSURL URLWithString:model.promotion_img] placeholderImage:[UIImage imageNamed:@"nopic"]];
        UILabel *label= [self.ShufflingScr.subviews objectAtIndex:i*2+3];
        label.text = model.title;
    }
}

/* 添加定时器方法 */
- (void)addTimer {
    if (self.timer) {
        [self.timer fire];
    }
    self.timer = [NSTimer scheduledTimerWithTimeInterval:7 target:self selector:@selector(nextPage) userInfo:nil repeats:YES];
}

- (void)nextPage {
    [self.ShufflingScr setContentOffset:CGPointMake((int)(self.ShufflingScr.contentOffset.x/Width+1)*Width , 0) animated:YES];
    if (self.ShufflingScr.contentOffset.x > Width * imgNum) {
        [self.ShufflingScr setContentOffset:CGPointMake(Width, 0) animated:NO];
    }
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    if (self.ShufflingScr.contentOffset.x > Width * imgNum) {
        [self.ShufflingScr setContentOffset:CGPointMake(Width, 0) animated:NO];
    }
    if (self.ShufflingScr.contentOffset.x < Width) {
        [self.ShufflingScr setContentOffset:CGPointMake(Width * imgNum, 0) animated:NO];
    }
}

//点击手势
- (void)click:(UITapGestureRecognizer *)tap{
    int a = (int)(self.ShufflingScr.contentOffset.x/Width);
    NSLog(@"这是第%d张图片", a);
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

@end
